package com.thoughtworks.yuyijq.generate.template;

import com.thoughtworks.yuyijq.generate.element.ImportElement;
import com.thoughtworks.yuyijq.generate.element.MethodElement;

import java.util.ArrayList;
import java.util.List;

/**
 * User: yuyijq
 * Date: Oct 10, 2010
 * Time: 10:14:39 AM
 */
public class Template {
    public List<ImportElement> getImportElements() {
        return importElements;
    }

    public List<MethodElement> getMethodElements() {
        return methodElements;
    }

    private List<ImportElement> importElements;

    private List<MethodElement> methodElements;
    private String templateResource;

    public Template(String templateResource) {
        this.templateResource = templateResource;

        importElements = new ArrayList<ImportElement>();
        methodElements = new ArrayList<MethodElement>();
    }

    public void read() {
        int readLength = 0;
        templateResource = templateResource.trim();
        while ((templateResource = templateResource.substring(readLength)).length() > 0) {
            if (templateResource.startsWith("import")) {
                ImportElement importElement = new ImportElement();
                readLength = importElement.read(templateResource)+1;
                importElements.add(importElement);
            } else {
                MethodElement methodElement = new MethodElement();
                readLength = methodElement.read(templateResource)+1;
                methodElements.add(methodElement);
            }
        }
    }

    public static void main(String[] args) {
        String templateResource = "/com/thoughtworks/yuyijq/generate/generateEqualsAndHashCode.vm";
        Template template = new Template(TemplateLoader.load(templateResource));
        template.read();
    }
}
